﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;
using YasharEl.Infrastructure.OrgUnit.EntityModel;
using YasharEl.Infrastructure.Core.PersistenceModel.NHibernate.Personification.UserTypes;
using YasharEl.Infrastructure.Core.Personification;

namespace YasharEl.Infrastructure.OrgUnit.PersistanceModel.NHibernate.Mappings
{
    public class EmployeeEntityMap : ClassMap<EmployeeEntity>
    {
        public EmployeeEntityMap()
        {
            ReadOnly();
            Table("tblemployeesdata");
            Id(e => e.EmployeeId)
                .Column("EMPLOYEESDATAID")
                .GeneratedBy.Assigned();
            Map(e => e.IdentityNumber)
                .Column("EMPLOYEEIDNUMBER")
                .Not.Nullable()
                .CustomType<IsraelIdentityNumberUserType>();
            Map(e => e.EmployeeNumber)
                .Column("EMPLOYEENUMBER")
                .Not.Nullable();
            Component<Name>(e => e.EnglishName,
                nameEntity =>
                {
                    nameEntity.Access.Property();
                    nameEntity.Map(n => n.Firstname)
                        .Column("FIRSTNAMEENGLISH")
                        .Not.Nullable();
                    nameEntity.Map(n => n.Lastname)
                        .Column("LASTNAMEENGLISH")
                        .Not.Nullable();
                }
            );
            Component<Name>(e => e.HebrewName,
                nameEntity =>
                {
                    nameEntity.Access.Property();
                    nameEntity.Map(n => n.Firstname)
                        .Column("FIRSTNAMEHEBREW")
                        .Not.Nullable();
                    nameEntity.Map(n => n.Lastname)
                        .Column("LASTNAMEHEBREW")
                        .Not.Nullable();
                }
            );
            Map(e => e.BirthDate)
                .Column("DATEOFBIRTH")
                .Not.Nullable();
            References(e => e.BelongsToUnit)
                .Column("UNITCODE")
                .LazyLoad()
                .Class<OrganizationalUnitEntity>();
            Component<Occupation>(e => e.CurrentOccupation,
                occupationEntity =>
                {
                    occupationEntity.Access.Property();
                    occupationEntity.Map(n => n.Code)
                        .Column("OCCUPATIONCODE")
                        .Not.Nullable();
                    occupationEntity.Map(n => n.Description)
                        .Column("OCCUPATIONDESCRIPTION")
                        .Not.Nullable();
                }
            );
            Map(e => e.HiredFrom)
                .Column("HIREDATEFROM")
                .Not.Nullable();
            Map(e => e.HiredUntil)
                .Column("HIREDATETO")
                .Nullable();
            References(e => e.DirectManager)
                .Column("MANAGERIDNUMBER")
                .LazyLoad()
                .PropertyRef(m => m.IdentityNumber)
                .Class<ManagerEntity>();
            Map(e => e.Username)
                .Column("USERNAME")
                .Not.Nullable();
            Map(e => e.Gender)
               .Column("GENDER")
               .Nullable()
               .CustomType<GenderEnumerationType>();
            Component<Job>(e => e.JobPosition,
                jobEntity =>
                {
                    jobEntity.Access.Property();
                    jobEntity.Map(n => n.Code)
                        .Column("JOBCODE")
                        .Not.Nullable();
                    jobEntity.Map(n => n.Description)
                        .Column("JOBDESCRIPTION")
                        .Not.Nullable();
                    jobEntity.Map(n => n.JobFrom)
                        .Column("JOBDATEFROM")
                        .Not.Nullable();
                    jobEntity.Map(n => n.JobTo)
                        .Column("JOBDATETO")
                        .Nullable();
                }
            );

            DiscriminateSubClassesOnColumn("", "0")
                .Formula(@"
(
case 
    when (select count(*) from tblemployeesdata t where t.manageridnumber = EMPLOYEEIDNUMBER) > 0 
    then 1 
    else 0
end
)
");
        }
    }
}
